package com.wxgo.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.upload.UploadFile;
import com.wxgo.utils.DateUtil;

public class UploadController extends Controller {

	public void upload() {
		String type = getPara("t");
		String uploadPath = "/upload/";
		if ("module".equals(type)) {
			uploadPath = "/module/";
		} else if ("industry".equals(type)) {
			uploadPath = "/industry/";
		}
		List<UploadFile> fileList = getFiles(uploadPath);
		String fileName = "";
		for (UploadFile f : fileList) {
			fileName = f.getFileName();
			fileName = DateUtil.getStringDateNumber() + "." + fileName.substring(fileName.lastIndexOf(".") + 1);
			f.getFile().renameTo(new File(f.getUploadPath() + fileName));
		}
		renderJson("json", new Ret().set("code", "-1").set("filename", "/upload" + uploadPath + fileName));
	}

	public void industry() {
		String uploadPath = "/industry/";
		List<UploadFile> fileList = getFiles(uploadPath);
		String fileName = "";
		for (UploadFile f : fileList) {
			fileName = f.getFileName();
			fileName = DateUtil.getStringDateNumber() + "." + fileName.substring(fileName.lastIndexOf(".") + 1);
			f.getFile().renameTo(new File(f.getUploadPath() + fileName));
		}
		renderJson(new Ret().set("code", "0").set("msg", "").set("data",
				new Ret().set("src", "/upload" + uploadPath + fileName)));

	}

	public void uploadImg() throws Exception {
		// 定义允许上传的文件扩展名
		HashMap<String, String> extMap = new HashMap<String, String>();
		extMap.put("image", "gif,jpg,jpeg,png,bmp");
		extMap.put("flash", "swf,flv");
		extMap.put("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
		extMap.put("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");

		Map<String, Object> result = new HashMap<String, Object>();
		String dirName = getPara("dir") == null ? "image" : getPara("dir");
		String realpath = "/news/file/";
		UploadFile uf = getFile("imgFile", realpath);
		String affix_id = "";
		String affix_name = "";
		if (uf != null) {
			affix_name = uf.getFile().getName();
			File file = uf.getFile();
			// 检查扩展名
			String fileExt = affix_name.substring(affix_name.lastIndexOf(".") + 1).toLowerCase();
			if (!Arrays.<String> asList(extMap.get(dirName).split(",")).contains(fileExt)) {
				result.put("error", 1);
				result.put("message", "上传文件扩展名是不允许的扩展名。\n只允许" + extMap.get(dirName) + "格式。");
				file.delete();
			} else {
				SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
				affix_id = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
				file.renameTo(new File(uf.getUploadPath() + affix_id));
				result.put("error", 0);
				result.put("url", getRequest().getContextPath() + "/upload" + realpath + affix_id);
			}
		} else {
			result.put("error", 1);
			result.put("message", "请选择文件");
		}
		renderJson(result);
	}
}
